/**
 * 
 */
package com.fyp.ptma.dao.repository;

import java.util.List;

import javax.persistence.PersistenceException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fyp.ptma.dao.repository.irepository.IGroupDao;
import com.fyp.ptma.model.Application;
import com.fyp.ptma.model.Downloader;
import com.fyp.ptma.model.Group;
import com.fyp.ptma.model.User;

/**
 * @author YahyaArshad
 * 
 */
@Repository
public class GroupDao implements IGroupDao {
	@Autowired
	private SessionFactory sessionFactory;

	public Session openSession() {
		return sessionFactory.getCurrentSession();
	}

	public Long save(Object objToSave) throws PersistenceException,
			ConstraintViolationException {
		// TODO Auto-generated method stub
		return null;
	}

	public Long update(Object objToUpdate) throws PersistenceException {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean delete(Object objToDelete) throws PersistenceException,
			ConstraintViolationException {
		// TODO Auto-generated method stub
		return false;
	}

	public List<Group> getAllGroups(Application application)
			throws HibernateException {
		Session session = this.openSession();
		String strQuery = "select g from Group g where g.user.email = :devEmail";
		Query query = session.createQuery(strQuery);
		@SuppressWarnings("unchecked")
		List<Group> groups = query.list();
		return groups;
	}

	public List<Downloader> getAllDownloader(Group group)
			throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Group> getAllUserGroups(String email) throws HibernateException {
		Session session = this.openSession();
		User user = (User) session.createCriteria(User.class)
				.add(Restrictions.eq("email", email)).uniqueResult();
		List<Group> groups = user.getGroups();
		System.out.println(groups.toString());
		return groups;
	}

	public long addDownloaderInGroup(Downloader downloader)
			throws ConstraintViolationException, PersistenceException,
			HibernateException {
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean deleteDownloaderFromGroup(Downloader downloader)
			throws ConstraintViolationException, HibernateException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void saveOrUpdate(Object objToSave) throws PersistenceException,
			ConstraintViolationException {
		// TODO Auto-generated method stub

	}

}
